<?php
	session_start();
	require('include/inc.start.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sell your books</title>
<?php include ('common_head.html');?>
<link href="css/sellbooks.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/sellbook.js"></script>
<?php
// connection settings stored in file
	include("include/connectionParameters.php");
	
	error_reporting(E_ALL);
	ini_set("upload_tmp_dir", getcwd()."\\temp");
?>
</head>

<body>
<?php include ('login.php');?>
<div class="back">	
  <div class="footerBg">
  <div id="frame">
  <?php include ('header.php');?>
  <div class="mainfrm">
  <?php
  // check if form was submitted, or just something we show for the first time
  if (isset($_POST['title'])){
    // form was submitted, getting information
	$title = $_POST['title'];
	$author = $_POST['author'];
	$isbn = $_POST['isbn'];
	$edition = $_POST['edition'];
	$format = $_POST['bookFormat'];
	$course = $_POST['course'];
	$professor = $_POST['professor'];
	$location = $_POST['location'];
	$dollars = $_POST['dollars'];
	$cents = $_POST['cents'];
	$description = $_POST['description'];
	$postDate = date("Y-m-d");
	$publisher = $_POST['publisher'];
	$yearPublished = $_POST['yearPublished'];
	$price = "$".$dollars.",".$cents;
	$condition = $_POST['bookCondition'];
	
	if ((($_FILES["bookPicture"]["type"] == "image/gif")
|| ($_FILES["bookPicture"]["type"] == "image/jpeg")
|| ($_FILES["bookPicture"]["type"] == "image/pjpeg"))
&& ($_FILES["bookPicture"]["size"] < 20000))
  {
	  echo "gonna upload to ". $_SERVER['DOCUMENT_ROOT'] ."\html\sample\bookImages\\" . $_FILES["bookPicture"]["name"];
	  
	if ($_FILES["bookPicture"]["error"] > 0){
	  echo "problem with book picture";	
	} else {
  	  move_uploaded_file($_FILES["bookPicture"]["tmp_name"],
     $_SERVER['DOCUMENT_ROOT'] ."\html\sample\bookImages\\". $_FILES["bookPicture"]["name"]);
	}
  } else {
	    echo "bad file";
	  }
	
   $connection = mysql_connect($host,$user,$pass)
		or die ("Can't connect to server, try again later, please");

	mysql_select_db($database);
	
	
	$query = "insert into `Books`(`Title`,`Author`,`ISBN`,`ConditionId`,`Edition`,`Publisher`,`YearPublished`,`BookFormat`,`Course`,`Professor`,`Location`,`Price`,`PostDate`,`Description`) values ('".$title."','".$author."','".$isbn."','".$condition."','".$edition."','".$publisher."','".$yearPublished."','".$format."','".$course."','".$professor."','".$location."','".$price."','".$postDate."','".$description."');";
	$result = mysql_query($query,$connection)
		or die ("Problem with adding information, please try again later.");
	// info was added
	
	echo "Your information was added, thank you.";
	mysql_close($connection);
  } else {
?>
  <div class="bluestripfrm_sell">
  <div class="blue_heading_sell">
  <h1>Sell your books </h1>
  </div>
  </div><!--bluestripfrm-->
<div id="pagecontent_sell">
<form id="sellBookForm" name="sellBookForm" method="post" enctype="multipart/form-data" action="sellBooks.php">
<table width="548">
  <tr>
    <td width="186" >Title:</td>
    <td width="310">
      <input type="text" name="title" id="title"  size="40" maxlength="40" />
      <p class="error" id="titleError" style="display:none"><br/> *Field can't be empty</p> 
    </td>
  </tr>
  <tr>
    <td>Author:</td>
    <td>
      <input name="author" type="text" id="author" size="40" maxlength="40" />
      <p class="error" id="authorError" style="display:none"><br/> *Field can't be empty</p> 
    </td>
  </tr>
  <tr>
    <td>ISBN:</td>
    <td><input name="isbn" type="text" id="isbn" value="" size="30" maxlength="30" />
    <p class="error" id="isbnError" style="display:none"><br/> *Field can't be empty</p> 
    </td>
  </tr>
   <tr>
    <td>Edition:</td>
    <td><input name="edition" type="text" id="edition" size="30" maxlength="30" /></td>
  </tr>
   <tr>
    <td>Publisher:</td>
    <td><input name="publisher" type="text" id="publisher" size="30" maxlength="30" /></td>
  </tr>
   <tr>
    <td>Year published:</td>
    <td><input name="yearPublished" type="text" id="yearPublished" size="4" maxlength="4" /></td>
  </tr>
  <tr>
    <td>Format:</td>
    <td><label>
      <select name="bookFormat" id="bookFormat">
      <option value="1"> Paperback </option>
      <option value="2"> Hardcover </option>
      <option value="3"> Other (e-book, audio cd, etc.) </option>
      </select>
    </label></td>
  </tr>
   <tr>
    <td>Condition:</td>
    <td><label>
      <select name="bookCondition" id="bookCondition">
      <option value="1"> Brand new </option>
      <option value="2"> Like new </option>
      <option value="3"> Very good </option>
      <option value="4"> Good </option>
      <option value="5"> Acceptable </option>
      </select>
    </label></td>
  </tr>
  <tr>
    <td>Course:</td>
    <td><input name="course" type="text" id="course" size="40" maxlength="40" /></td>
  </tr>
  <tr>
    <td>Professor:</td>
    <td><input name="professor" type="text" id="professor" size="30" maxlength="30" /></td>
  </tr>
   <tr>
    <td>Your location: <br/>
      ex: &quot;Boston, MA&quot; or &quot;MA&quot;</td>
    <td><input name="location" type="text" id="location" size="20" maxlength="20" /></td>
  </tr>
  <tr>
    <td>Description: </td>
    <td>
      <textarea name="description" id="description" cols="40" rows="10" onkeydown="charCounter(this.form.description, this.form.descriptionLength, 500)" onkeyup="charCounter(this.form.description, this.form.descriptionLength, 500)"></textarea> <br/>
      <input readonly type="text" size=3 value="500" name="descriptionLength"/> characters left
    </td>
  </tr>
    <tr>
    <td width="186">Book picture: (optional)</td>
    <td width="310">
     <input name="bookPicture" type="file"> <br/>
     (*.gif, *.jpeg)
    </td>
  </tr>
  <tr>
    <td>Price:</td>
    <td>$
      <input name="dollars" type="text" id="dollars" size="10" maxlength="7" /> .
      <input name="cents" type="text" id="cents" size="2" maxlength="2" />
      <p class="error" id="dollarsError" style="display:none"> <br/> *Field can't be empty</p> 
      </td>
  </tr>
  <tr>
    <td colspan="2" align="center">
<input type="submit" name="Submit" id="submit" value="Submit" onclick="return validateFields(this);" />
      </td>
  </tr>
</table>
    </form>
    </div> <!-- sell books -->
    <?php
} 
?>  
  <?php include("footer.php"); ?>
</div><!--mainfrm -->
   </div><!--frame-->
 </div><!--footerBg-->
</div><!--back -->
</body>
</html>
